﻿using System.Windows;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static void AssignWildcardExpression()
        {
            if (Globals.selectedEditor == null)
            {
                MessageBox.Show("当前未打开任何文档！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (Globals.selectedEditor.SelectedExpression == null)
            {
                MessageBox.Show("请先选中要指定为通配式的公式！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            if (Globals.selectedEditor.SelectedExpression is Expressions.SubPanel)
            {
                MessageBox.Show("子面板不能被指定为通配式！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Expressions.Expression curEx = Globals.selectedEditor.SelectedExpression;
            if (curEx is Expressions.BaseText)
            {
                Expressions.BaseText curBt = curEx as Expressions.BaseText;
                if (curBt.IsCollapsed)
                {
                    MessageBox.Show("折叠的标题文本块不允许被指定为通配式。此外“剪切、粘贴、删除、平移、拖动、更改缩进、对齐、嵌入”等操作亦被禁止。",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }

            //TODO 节点太多时提示。

            Globals.wildcardExpressionXmlString =
                Globals.selectedEditor.SelectedExpression.XmlData.OuterXml;
        }

        public static void InsertWildcardExpression()
        {
            Expressions.Expression curEx;

            if (Globals.selectedEditor == null)
            {
                MessageBox.Show("当前没有文档处于可编辑状态！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (Globals.selectedEditor.SelectedExpression == null)
            {
                MessageBox.Show("请先选中一个公式作为添加位置，通配式将出现在此公式之后的位置！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (Globals.wildcardExpressionXmlString == null ||
                Globals.wildcardExpressionXmlString.Length <= 0)
            {
                MessageBox.Show("尚未指定通配式！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            string exXmlString = Globals.wildcardExpressionXmlString;

            Expressions.SubPanel curPanel = Globals.selectedEditor.SelectedExpression as Expressions.SubPanel;
            if (curPanel != null)
            {
                curEx = curPanel.LastVisibleChildEx;
            }
            else
            {
                curEx = Globals.selectedEditor.SelectedExpression;
            }

            ModifingItem mi = new ModifingItem();

            Expressions.BaseText curBt = curEx as Expressions.BaseText;
            if (curBt != null && curBt.IsCollapsed)
            {
                Action actIsCollapsed = new Action(curBt.ExID, -1, curBt.GetType().Name, "IsCollapsed",
                    "True", "False");
                curBt.IsCollapsed = false;
                mi.Add(actIsCollapsed);
            }

            Expressions.Expression newEx = Command.InsertExpressionByXmlString(false, exXmlString, curEx);
            if (newEx != null)
            {
                Expressions.Expression.RebuildAllExIDs(newEx);

                Action action = new Action(newEx.ExID, newEx.ParentPanel.BasePanel.Children.IndexOf(newEx),
                    newEx.GetType().Name, "InsertExpression", null, newEx.XmlData.OuterXml,
                    ActionType.Insert, newEx.ParentPanel.ExID);
                mi.Add(action);

                newEx.Editor.Do(mi);

                Expressions.BaseText bt = newEx as Expressions.BaseText;
                if (bt != null)
                {
                    if (bt.IsHeaderText)
                    {
                        bt.ParentPanel.RefreshAutoNumbersOfBaseText();
                    }

                    Globals.RefreshToolBarItemsWhenExpressionSelected(Globals.MainWindow);
                }
            }
        }
    }
}
